# import pymysql
# #创建连接对象和游标对象
# conn = pymysql.connect(user = 'root', password= 'root', database = 'advanced', charset= 'utf8')
# cursor = conn.cursor()
# name = input('姓名:')
# sql ='SELECT * FROM student WHERE stu_name = %s'
# print(sql)
# rows = cursor.execute(sql, [name])
# if rows:
#     students = cursor.fetchall()
#     print(students)
# else:
#     print('没有要找的学生')
# cursor.close()
# conn.close()
import pymysql
class DB:
    def __init__(self,databese, host = 'localhost', user= 'root',password = 'root',port=3306, charset = 'utf8',
                 cursor_type=pymysql.cursors.Cursor):
        self.__conn = pymysql.connect(host=host, user=user, password=password, port=port, charset=charset,
                                      database=database)
        self.__cursor = self.__conn.cursor(cursor_type)
    def read(self, sql, pranms_list=[]):
        try:
            rows = self.__cursor.execute(sql, pranms_list)
            data = self.__cursor.fetchall()
        except Exception as e:
            print('sql语句有问题')
            return None, 'No Data'
        return rows, data
    def write(self, sql):
        try:
            rows = self.__cursor.execute(sql)
        except Exception as e:
            self.__conn.rollback()
            return 0
        else:
            #提交
            self.__conn.commit()
        return rows
    def __del__(self):
        #对象销毁后执行的方法
        self.__cursor.close()
        self.__conn.close()
if __name__ == '__main__':
    db = DB('advanced', cursor_type=pymysql.cursors.DictCursor)
    # rows, data = ab.read('select * from student')
    #print(data)
    name = input('name:')
    rows, data = db.read(f'select * from student where stu_name = %s', [name])
    print(data)
    # db = DB('adcanced', cursor_type=pymysql.cursors.DictCursor)
    # rows, data = db.read('select * from student')
    # if rows:
    #     print(rows, data)
    # # 写操作
    # rows = db.write("insert into student(stu_no, stu_name) VALUES('itsrc -017', '周杰伦1')")
    # if rows:
    #     print('写入成功')






